package com.example.demo.controller;

import com.example.demo.dao.ExampleRepository;
import com.example.demo.entity.ExampleEntity;
import com.example.demo.mapper.DemoMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by Q-ays.
 * whosqays@gmail.com
 * 07-12-2018 23:08
 */
@RestController
public class IndexController {

    @Autowired
    ExampleRepository er;

    @Autowired
    DemoMapper dm;

//    @RequestMapping("")
//    public String index() {
//        return "hello";
//    }


    @GetMapping("/data")
    public String data() {
        StringBuilder sb = new StringBuilder();

        List<ExampleEntity> exampleEntities = er.findAll();

        for (ExampleEntity ee : exampleEntities) {
            sb.append(ee.toString());
            sb.append("\n");
        }

        return sb.toString();
    }

    @GetMapping("/mybatis-mix")
    public String mix() {
        List<Map> list = dm.mixSelect();

        return new JSONArray(list).toString();
    }

    @GetMapping("/mybatis/{param}")
    public String param(@PathVariable String param){
        List<Map> list = dm.selectWithParam(param);

        return new JSONArray(list).toString();
    }

    @GetMapping("/mybatis")
    public String param1(String param) {
        List<Map> list = dm.selectWithParam(param);
        return new JSONArray(list).toString();
    }

}
